﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using MathematicallySafe.HeadCounter.Data.Common;
using MathematicallySafe.HeadCounter.Data;
using MathematicallySafe.HeadCounter.Data.Interfaces;
using MathematicallySafe.HeadCounter.Services;
using StructureMap;

namespace MathematicallySafe.HeadCounter.ViewModels
{
    public class ReportsViewModel: ViewModelBase
    {
        private static readonly IGuild Guild = ObjectFactory.GetInstance<IGuild>();

        private readonly List<string> _reports = new List<string>
                                                 {
                                                     "Attendance",
                                                     "Loot Priority"
                                                 };

        private string _selectedReport;

        public ReportsViewModel()
        {
            _selectedReport = _reports.FirstOrDefault();
        }

        public IEnumerable<string> Reports
        {
            get { return _reports; }
        }

        public string SelectedReport
        {
            get { return _selectedReport; }
            set
            {
                _selectedReport = value;
                OnPropertyChanged("SelectedReport");
            }
        }

        private ObservableCollection<AttendanceDatum> sixWeekAttendanceData;

        public ObservableCollection<AttendanceDatum> SixWeekAttendanceData
        {
            get
            {
                if (sixWeekAttendanceData == null)
                {
                    sixWeekAttendanceData = new ObservableCollection<AttendanceDatum>();

                    var top10Attenders = (Guild.ActiveRaidersByMain.OrderBy(r => r.ShortPeriodAttendance)).Take(10);

                    top10Attenders.ForEach(r => sixWeekAttendanceData.Add(new AttendanceDatum
                                                     {
                                                         RaiderName = r.MainName,
                                                         Attendance = r.ShortPeriodAttendance * 100.0
                                                     }));
                }

                return sixWeekAttendanceData;
            }
        }
    }

    public class AttendanceDatum
    {
        private static readonly IOptions Options = ObjectFactory.GetInstance<IOptions>();

        public string RaiderName { get; set; }
        public double Attendance { get; set; }
        public SolidColorBrush Colour
        {
            get
            {
                if (Attendance >= Options.HighAttendancePercentage)
                {
                    return System.Windows.Application.Current.Resources["HighAttendanceColour"] as SolidColorBrush;
                }

                if (Attendance >= Options.GoodAttendancePercentage)
                {
                    return System.Windows.Application.Current.Resources["GoodAttendanceColour"] as SolidColorBrush;
                }

                if (Attendance >= Options.FairAttendancePercentage)
                {
                    return System.Windows.Application.Current.Resources["FairAttendanceColour"] as SolidColorBrush;
                }

                return System.Windows.Application.Current.Resources["LowAttendanceColour"] as SolidColorBrush;
            }
        }
    }
}
